# 设置UTF-8编码
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

# 设置签名凭据
$env:KEYSTORE_PASS = '123456'
$env:KEY_PASS = '123456'

# 读取pubspec版本号
$version = (Get-Content pubspec.yaml -Raw) -match 'version: (.+)' | Out-Null; $matches[1]

# 构建流程
try {
    Write-Host "[1/3] 清理构建缓存..." -ForegroundColor Cyan
    flutter clean

# 深度清理Gradle缓存
    cd android
    .\gradlew clean
    cd ..

    Write-Host "[2/3] 构建Release版APK..." -ForegroundColor Cyan
        # 带时间统计的构建命令
    $stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
    flutter build apk --release `
    # --keystore "$PSScriptRoot/android/app/mathfun-keystore.jks" `
    # --store-password $env:KEYSTORE_PASS `
    # --alias mathfun `
    # --key-password $env:KEY_PASS `
    # --verbose `
    # --no-tree-shake-icons `
    # --build-name="$version" `
    # --build-number="$(Get-Date -Format 'yyMMddHH')"
    # 统一密钥库路径声明

    $stopwatch.Stop()

    Write-Host "[3/3] 构建完成! 耗时: $($stopwatch.Elapsed.ToString('hh\:mm\:ss'))" -ForegroundColor Green
    Write-Host "APK版本: $version"
    Write-Host "输出路径: $(Resolve-Path 'build\app\outputs\apk\release\app-release.apk' -ErrorAction SilentlyContinue)" -ForegroundColor Cyan
}

catch {
    Write-Host "`n构建失败: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}